package main

func Search(s rune, r []rune) (bool, int) {
	for i, k := range r {
		if k == s {
			return true, i
		}
	}
	return false, len(r)
}

func lengthOfLongestSubstring(s string) int {
	runes := []rune(s)
	l := len(s)
	if l == 0 || l == 1 {
		return l
	}
	var head, tail int = 0, 1
	var sum int = 1
	var tmp int
	for {
		if tail == l {
			if l-head > sum {
				sum = l - head
			}
			break
		}
		check, i := Search(runes[tail], runes[head:tail])
		if check {
			index := i + head
			if index >= (tail-index)/2+head {
				tmp = index - head
			} else {
				tmp = tail - index
			}
			if tmp > sum {
				sum = tmp
			}
			head = index + 1
			tail++
			//fmt.Println(sum)
			continue
		}
		tail++
		if tail-head > sum {
			sum = tail - head
		}
		//fmt.Println(sum)
	}
	return sum
}
